package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dianping.R;
import com.example.entity.Goods;
import com.example.entity.Shop;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.squareup.picasso.Picasso;

public class GoodsDetailActivity extends Activity {
	private Goods goods;
	@ViewInject(R.id.goods_image)
	private ImageView goods_image;
	@ViewInject(R.id.goods_title)
	private TextView goods_title;
	@ViewInject(R.id.goods_desc)
	private TextView goods_desc;
	@ViewInject(R.id.goods_price)
	private TextView goods_price;
	@ViewInject(R.id.goods_old_price)
	private TextView goods_old_price;

	@ViewInject(R.id.shop_title)
	private TextView shop_title;
	@ViewInject(R.id.shop_phone)
	private TextView shop_phone;
	@ViewInject(R.id.shop_address)
	private TextView shop_address;
	@ViewInject(R.id.shop_call)
	private ImageView shop_call;

	@ViewInject(R.id.tv_more_details_web_view)
	private WebView tv_more_details_web_view;
	@ViewInject(R.id.wv_gn_warm_prompt)
	private WebView wv_gn_warm_prompt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tuan_goods_detail);
		ViewUtils.inject(this);

		goods_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		// 设置内容的宽度自适应屏幕的宽度
		WebSettings webSetting1 = tv_more_details_web_view.getSettings();
		webSetting1.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		WebSettings webSetting2 = wv_gn_warm_prompt.getSettings();
		webSetting2.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			goods = (Goods) bundle.get("goods");
		}

		if (goods != null) {
			updateTitleImage();
			updateGoodsInfo();
			updateShopInfo();
			updateMoreDetails();
		}
	}

	// 解析网页内容显示到WebVIew中
	private void updateMoreDetails() {
		String[] data = htmlSub(goods.getDetail());
		tv_more_details_web_view.loadDataWithBaseURL("", data[0], "text/html",
				"utf-8", "");
		wv_gn_warm_prompt.loadDataWithBaseURL("", data[1], "text/html",
				"utf-8", "");
	}

	public String[] htmlSub(String html) {
		char[] str = html.toCharArray();
		int len = html.length();
		System.out.println("html.length()" + len);
		int n = 0;
		String[] data = new String[3];
		int oneIndex = 0;
		int secIndex = 1;
		int thiIndex = 2;

		for (int i = 0; i < len; i++) {
			if (str[i] == '【') {
				n++;
				if (n == 1)
					oneIndex = i;
				if (n == 2)
					secIndex = i;
				if (n == 3)
					thiIndex = i;
			}
		}

		if (oneIndex > 0 && secIndex > 1 && thiIndex > 3) {
			data[0] = html.substring(oneIndex, secIndex);
			data[1] = html.substring(secIndex, thiIndex);
			data[2] = html.substring(thiIndex, html.length() - 6);
		}
		return data;
	}

	private void updateShopInfo() {
		Shop shop = goods.getShop();
		shop_title.setText(shop.getName());
		shop_phone.setText(shop.getTel());

	}

	// 注册拨打电话
	@OnClick({R.id.shop_call,R.id.goods_detail_back})
	public void call(View view) {
		switch (view.getId()) {
		case R.id.shop_call:
			Intent caIntent = new Intent(Intent.ACTION_CALL);
			caIntent.setData(Uri.parse("tel:" + goods.getShop().getTel()));
			startActivity(caIntent);
			break;
		case R.id.goods_detail_back:
			finish();
		default:
			break;
		}
	}

	private void updateGoodsInfo() {
		goods_title.setText(goods.getSortTitle());
		goods_desc.setText(goods.getTitle());
		goods_price.setText("￥" + goods.getPrice());
		goods_old_price.setText("￥" + goods.getValue());// 原价
	}

	private void updateTitleImage() {
		Picasso.with(this).load(goods.getImgUrl())
				.placeholder(R.drawable.empty_photo).into(goods_image);
	}
}
